package com.example.luo.toggle;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class ToggleView extends View {
	private Bitmap bgBitmap;
	private Bitmap slideBitmap;
	private Paint paint;
	private float currentX;
	private int bgWidth;
	private int slideWidth;
	private static final int STATE_DOWN = 1;
	private static final int STATE_MOVE = 2;
	private static final int STATE_UP = 3;
	private int currentState = STATE_UP;//记录当前的触摸状态，默认是up
	private boolean flag = false; //默认的开关标识

	public ToggleView(Context context) {
		this(context,null);
	}

	public ToggleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		//获取两个图片的bitmap
		bgBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.switch_background);
		slideBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.slide_button_background);
		paint = new Paint();
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		System.out.println("onMeasure");
		//获取两个图片的宽度
		slideWidth = slideBitmap.getWidth();
		bgWidth = bgBitmap.getWidth();
		int heiht = bgBitmap.getHeight();
		setMeasuredDimension(bgWidth, heiht);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			//按下后判断
			currentX = event.getX();
			//改变状态
			currentState = STATE_DOWN;
			break;
		case MotionEvent.ACTION_MOVE:
			currentX = event.getX();
			currentState = STATE_MOVE;
			break;
		case MotionEvent.ACTION_UP:
			currentState = STATE_UP;
			
			break;
		}
		//触发onDraw方法
		invalidate();
		return true;//自己处理事件
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		System.out.println("onFinishInflate");
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		System.out.println("onLayout");
	}

	@Override
	protected void onDraw(Canvas canvas) {
		System.out.println("onDraw");
		canvas.drawBitmap(bgBitmap, 0, 0, paint );
		switch (currentState) {
		case STATE_DOWN:
		case STATE_MOVE:
			downOrMove(canvas);
			break;
		case STATE_UP:
			//弹起的时候，判断是在哪边,再做一个事件的标识，当
			/*if ((currentX - bgWidth / 2) < (bgWidth - slideWidth) / 2) {
				canvas.drawBitmap(slideBitmap, 0, 0, paint);
				if (flag) {//开关已经处于打开状态才
//					Toast.makeText(getContext(), "开关关闭了", 0).show();
					mToggleStateChanged.openOrClose(this,flag);
					flag = !flag;
				}
			}else{
				canvas.drawBitmap(slideBitmap, bgWidth - slideWidth, 0, paint);
				if (!flag) {
//					Toast.makeText(getContext(), "开关打开了", 0).show();
					mToggleStateChanged.openOrClose(this,flag);
					flag = !flag;
				}
			}*/
			if ((currentX - slideWidth / 2) < (bgWidth - slideWidth) / 2) {
				canvas.drawBitmap(slideBitmap, 0, 0, paint);
				if (flag) {//开关已经处于打开状态才
//					Toast.makeText(getContext(), "开关关闭了", 0).show();
					mToggleStateChanged.openOrClose(this,flag);
					flag = !flag;
				}
			}else{
				canvas.drawBitmap(slideBitmap, bgWidth - slideWidth, 0, paint);
				if (!flag) {
//					Toast.makeText(getContext(), "开关打开了", 0).show();
					mToggleStateChanged.openOrClose(this,flag);
					flag = !flag;
				}
			}
			break;
		}
	}
	//按下或者移动的时候重新绘画
	private void downOrMove(Canvas canvas) {
		if (currentX < slideWidth / 2) {
			canvas.drawBitmap(slideBitmap, 0, 0, paint );
		}else if ((currentX - slideWidth/2) > (bgWidth - slideWidth)) {
			canvas.drawBitmap(slideBitmap, (bgWidth - slideWidth), 0, paint );
		}else{
			canvas.drawBitmap(slideBitmap, currentX - slideWidth / 2, 0, paint );
		}
		
		/*if (currentX < bgWidth / 2) {
			canvas.drawBitmap(slideBitmap, 0, 0, paint );
		}else if ((currentX - bgWidth / 2) > (bgWidth - slideWidth)) {
			canvas.drawBitmap(slideBitmap, (bgWidth - slideWidth), 0, paint );
		}else{
			canvas.drawBitmap(slideBitmap, currentX - bgWidth / 2, 0, paint );
		}*/
	}
	
	//提供一个接口处理开关打开和关闭的事件
	public interface OnToggleStateChanged{
		void openOrClose(ToggleView toggle, boolean isOpened);
	}
	
	public OnToggleStateChanged mToggleStateChanged;
	
	public void processOnToggleStateChanged(OnToggleStateChanged toggleStateChanged){
		this.mToggleStateChanged = toggleStateChanged;
	}
}
